package net.googlecode.kharchenko.domains;


import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.ManyToOne;
import javax.persistence.ManyToMany;
import javax.persistence.JoinColumn;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.JoinTable;
import java.util.Set;
import java.util.TreeSet;

/**
 * Class, that contains information about user.
 *
 * @author Kharchenko Yaroslav
 */
@Entity
@Table(name = "users")
public class Person {

    /**
     * Default rate of user.
     */
    public static final int DEFAULT_RATE = 100;



    /**
     * Unique identificator.
     */
    @Id
    @Column(name = "id")
    @GeneratedValue()
    private Long id;


    /**
     * Second name of user.
     */
    @Column(name = "sname")
    private String sname;

    /**
     * Is user active.
     */
    @Column(name = "enabled")
    private boolean enabled = true;

    /**
     * Role of the user: ROLE_USER, ROLE_ADMIN,etc.
     */
    @Column(name = "authority")
    private String authority = "ROLE_USER";

    /**
     * First name of user.
     */
    @Column(name = "fname")
    private String fname;


    /**
     * Patronymic  of user.
     */
    @Column(name = "tname")
    private String tname;

    /**
     * User's email.
     */
    @Column(name = "email")
    private String email;

    /**
     * User's password.
     */
    @Column(name = "password")
    private String password;


    /**
     * Rate of user.
     */
    @Column(name = "rate")
    private Integer rate = DEFAULT_RATE;


    /**
     * Name of user avatar image.
     */
    @Column(name = "imgurl")
    private String imgurl = "no.jpg";

    /**
     * Group in which user study.
     */
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "grpid")
    private Group group;

    /**
     * Set of persons who voted for user.
     */
    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "votes")
    private Set<Person> votes ;


    /**
     * Get persons who voted for user.
     *
     * @return persons who voted for user.
     */
    public Set<Person> getVotes() {
        return votes;
    }

    /**
     * Set persons who voted for user.
     *
     * @param votes persons who voted for user
     */
    public void setVotes(Set<Person> votes) {
        this.votes = votes;
    }

    /**
     * Check is person account enabled.
     *
     * @return is person account enabled.
     */
    public boolean isEnabled() {
        return enabled;
    }

    /**
     * Set is user account enabled.
     *
     * @param enabled is user account enabled
     */
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    /**
     * Get unique identificator of user.
     *
     * @return unique identificator.
     */
    public Long getId() {
        return id;
    }

    /**
     * Set unique identificator of user.
     *
     * @param id new identificator
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * Get second name of user.
     *
     * @return second name of user.
     */
    public String getSname() {
        return sname;
    }

    /**
     * Set second name  of user.
     *
     * @param sname second name of user
     */
    public void setSname(String sname) {
        this.sname = sname;
    }

    /**
     * Get role of user.
     *
     * @return role of user.
     */
    public String getAuthority() {
        return authority;
    }

    /**
     * Set role of user.
     *
     * @param authority role of user
     */
    public void setAuthority(String authority) {
        this.authority = authority;
    }

    /**
     * Get first name of user.
     *
     * @return first name of user.
     */
    public String getFname() {
        return fname;
    }

    /**
     * Set first name of user.
     *
     * @param fname first name of user
     */
    public void setFname(String fname) {
        this.fname = fname;
    }

    /**
     * Get patronymic of user.
     *
     * @return patronymic of user.
     */
    public String getTname() {
        return tname;
    }

    /**
     * Set patronymic of user.
     *
     * @param tname patronymic of user
     */
    public void setTname(String tname) {
        this.tname = tname;
    }

    /**
     * Get email of user.
     *
     * @return email of user.
     */
    public String getEmail() {
        return email;
    }

    /**
     * Set email of user.
     *
     * @param email email of user
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Get password of user.
     *
     * @return password of user.
     */
    public String getPassword() {
        return password;
    }

    /**
     * Set password of user.
     *
     * @param password user's password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Get user's group.
     *
     * @return user's group.
     */
    public Group getGroup() {
        return group;
    }

    /**
     * Set user's group.
     *
     * @param group user's group
     */
    public void setGroup(Group group) {
        this.group = group;
    }

    /**
     * Get rate of user.
     *
     * @return rate of user.
     */
    public Integer getRate() {
        return rate;
    }

    /**
     * Set rate of user.
     *
     * @param rate user's rate
     */
    public void setRate(Integer rate) {
        this.rate = rate;
    }

    /**
     * Get name of user avatar image.
     *
     * @return email of user.
     */
    public String getImgurl() {
        return imgurl;
    }

    /**
     * Set name of user's avatar image.
     *
     * @param imgurl name of user's avatar image
     */
    public void setImgurl(String imgurl) {
        this.imgurl = imgurl;
    }


   @Override public boolean equals(Object o) {
    boolean result = false;
    if (o instanceof Person) {
        Person p = (Person) o;
        result = id.equals(p.getId());
    }
    return result;
}

       @Override public int hashCode() {
        return id.hashCode();
    }

}
